'''
Module : storage
Created on Jul 26, 2012

@author: zhong

Description : 
work for monitor  host resource 
model storage
'''
import time,re
import threading

DEBUG = True

class device_stats(  threading.Thread  ):
    def __init__( self , tnum, tinterval):
        self.start_time = time.time()
        
        threading.Thread.__init__(self)
        self.thread_num = tnum
        self.thread_interval = tinterval    
        self.thread_stop = False
        
        self.logging_buff = []
        
        self.state_read()
                
            
    def record_add(self, record):
        #print record
        self.logging_buff.append(record)
        #print  len(self.logging_buff)
        
    def state_read(self):
        logging_time = time.time()
        diskstats = []
        
        fd = open("/proc/diskstats","r")
                
        for line in fd.readlines():
            field = line.split()
            
            if re.match(r'^sd[a-z]$|sd[a-z][a-z]$',field[2]):
                device_name = field[2]
                rio = field[3]
                rsec = field[5]
                wio = field[7]
                wsec = field[9]
                diskstats.append( (logging_time, device_name, float(rio), float(rsec), float(wio), float(wsec)) )
                
        self.record_add(diskstats)
        
        fd.close()
        
    def compute(self):
        if len(self.logging_buff) < 2 :
            time.sleep(self.thread_interval)
            self.state_read()
        else :
            self.state_read()
            time.sleep(self.thread_interval)
            self.logging_buff.reverse()
            self.logging_buff.remove()
            print self.logging_buff
            
            
        
    
    def show(self):
        for records in self.logging_buff:
            for line in records:
                print line
        #for line in self.diskstats:
           # print line
           

    def run(self):
        while not self.thread_stop:
            print 'just testing %d' % (self.thread_num)
            time.sleep(self.interval)
    
    def debug(self):
        for record in self.logging_buff:
            for line in record:
                print line
        print  " %f ms" % ((time.time() - self.start_time) *1000)


    def stop(self):
        self.thread_stop = True

        
    def  __del__(self):
        if DEBUG:
            self.debug()
        


"""
def mon_disk_stats(current_stat , interval):
    diskinfo_lst = []
    
    time.sleep(interval)
    
    
    #device_id ,  read_io_request, read_KB, write_io_request, write_KB
    for i in range(0,   len(current_stat) ):
        diskinfo_lst.append((
                              current_stat[i][0] ,  
                              ((future_stat[i][1] - current_stat[i][1]) / interval ) ,
                              ((future_stat[i][2] - current_stat[i][2]) / 2 / interval ),
                              ((future_stat[i][3] - current_stat[i][3]) / interval ),
                              ((future_stat[i][4] - current_stat[i][4]) / 2 / interval ) 
                              ))
                
    return diskinfo_lst
    
def show_disk_stats( disk_stats ) :
    for line in disk_stats :
        print line
    


        
    
"""